package cn.moukou.tongji.ihealth.predication.listener;

import cn.moukou.tongji.ihealth.predication.service.NettyService;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class PredicationListener {

    @Autowired
    NettyService nettyService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "ihealth.predication.output", declare = "true"),
                    exchange = @Exchange(
                            value = "ihealth.exchange",
                            ignoreDeclarationExceptions = "true"
                    ),
                    key = {"ihealth.predication.output"}
            )
    )
    public void listen(String msg) {
        System.out.println("接受到消息" + msg);
        Map<String, Object> map = new HashMap<>();
        map = JSONObject.parseObject(msg);
        String predication = map.get("predication").toString();
        String uid = map.get("uid").toString();
        nettyService.pushPredictionToUser(uid, predication);
    }
}